//入库记录列表
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/ininventory/index',
                    add_url: 'warehouse/ininventory/add',
                    del_url: 'warehouse/ininventory/delete',
                    multi_url: 'warehouse/ininventory/multi',
                    table: 'in_inventory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'inorderid', title: __('Inorderid')},
                        {field: 'contractNo', title: __('ContractNo'),operate:false},
                        {field: 'trackorderid', title: __('Trackorderid'),operate:false},
                        {field: 'goods_count', title: '申请入库数',operate:false},
                        {field: 'pw_count', title: '已入库数',operate:false},
                        {field: 'left_count', title: '未入库数',operate:false},
                        {field: 'create_time', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'items', title: __('ItemInfo'),operate:false,formatter:Controller.api.formatter.itemFormat,events:Controller.api.events.getItems},
                        {field: 'status', title: __('Status'),formatter: Controller.api.formatter.checkstatus,searchList: Controller.api.getStatus},
                        {field: 'warehouse_code', title: __('warehouse_code'),searchList: Controller.api.getWMS, operate: 'FIND_IN_SET'},
                        {field: 'remark', title: __('Remark'),operate:false},
                      //   {
                      //     field: 'buttons',
                      //     width: "120px",
                      //     title: __('按钮组'),
                      //     table: table,
                      //     events: Table.api.events.operate,
                      //     buttons: [
                      //         {
                      //             name: 'detail',
                      //             text: __('弹出窗口打开'),
                      //             title: __('弹出窗口打开'),
                      //             classname: 'btn btn-xs btn-primary btn-dialog',
                      //             icon: 'fa fa-list',
                      //             url: 'example/bootstraptable/detail',
                      //             callback: function (data) {
                      //                 Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                      //             },
                      //             visible: function (row) {
                      //                 //返回true时按钮显示,返回false隐藏
                      //                 return true;
                      //             }
                      //         },
                      //         {
                      //             name: 'ajax',
                      //             text: __('发送Ajax'),
                      //             title: __('发送Ajax'),
                      //             classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                      //             icon: 'fa fa-magic',
                      //             url: 'example/bootstraptable/detail',
                      //             confirm: '确认发送',
                      //             success: function (data, ret) {
                      //                 Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                      //                 //如果需要阻止成功提示，则必须使用return false;
                      //                 //return false;
                      //             },
                      //             error: function (data, ret) {
                      //                 console.log(data, ret);
                      //                 Layer.alert(ret.msg);
                      //                 return false;
                      //             }
                      //         },
                      //         {
                      //             name: 'addtabs',
                      //             text: __('新选项卡中打开'),
                      //             title: __('新选项卡中打开'),
                      //             classname: 'btn btn-xs btn-warning btn-addtabs',
                      //             icon: 'fa fa-folder-o',
                      //             url: 'example/bootstraptable/detail'
                      //         }
                      //     ],
                      //     formatter: Table.api.formatter.buttons
                      // },
                      {
                          field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                          buttons: [
                               {
                                name: 'detail',
                                title: __('详情'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'warehouse/ininventory/viewDetail',
                                callback: function (data) {
                                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                },
                            },
                          //   {
                          //     name: 'customer_edit',
                          //     title: __('编辑'),
                          //     classname: 'btn btn-xs btn-success btn-editone',
                          //     icon: 'fa fa-pencil',
                          //     url: 'warehouse/ininventory/viewDetail111',
                          //     callback: function (data) {
                          //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                          //     },
                          // }
                        ],
                          formatter: Table.api.formatter.operate
                      }
                    ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                exportDataType: 'selected',
                exportTypes: ['doc', 'excel']
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);

            // $btn = $('.btn-editone');
            // console.log($btn);

            $(".btn-editone").each(function(){
              // $(this).click(function(){
              //   alert($(this).text());
              // });
              // $(this).attr("href","99999");
            });

            // $(document).on('click','.btn-editone',function () {
            //   debugger;
            //   console.error('9999999999');
            //   alert('9987');
            // });

            $(document).on('click','.add-track',function () {
                var that = this;
                var ids = Table.api.selectedids(table);

                var url = "warehouse/ininventory/addTrackNumber";
                ids = ($.isArray(ids) ? ids.join(",") : ids);
                url = Table.api.replaceurl(url, {ids:ids}, table);
                Fast.api.open(url, '关联追踪号', $(that).data() || {});

                
            });

            $(document).on('click','.print_inventory',function (){
                var that = this;
                var ids = Table.api.selectedids(table);

                var url = "warehouse/ininventory/printInventory?ids="+ids;
                //ids = ($.isArray(ids) ? ids.join(",") : ids);
                //url = Table.api.replaceurl(url,{ids:ids},table); window.alert(url);
                url = Fast.api.fixurl(url);
                //for(var i=3;i>0;i--){
                    //$.get(url);
                    window.location = url;
                //}
                return;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            debugger;
            Controller.api.bindevent();
        },
        addtracknumber: function () {
            Controller.api.bindevent();
        },
        printinventory: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            getWMS:function(){
                var warehouseOption=document.getElementById("warehouse_code");
                warehouseOption.parentNode.removeChild(warehouseOption);
                return warehouseOption.innerHTML;
            },
            getStatus:function(){
                return '<select class="form-control" name="warehouseid">'
                        +'<option value="1">审核中</option>'
                        +'<option value="1000">已审核</option>'
                        +'<option value="2000">部分入库</option>'
                        +'<option value="3000">已确认</option>'
                        +'</select>'
            },
            formatter: {
                checkstatus: function (value, row, index) {
                  if(value == 1000){
                      return '<span class="status_audit">已审核</span>';
                  }else if(value == 2000){
                      return '<span class="status_reject">部分入库</span>';
                  }else if(value == 2001){
                      return '<span class="status_reject">已拒绝</span>';
                  }else if(value == 3000){
                      return '<span class="status_checked">已确认</span>';
                  }else{
                      return '<span class="status_checking">审核中</span>';
                  }

                },
                itemFormat: function (value,row,index) {
                    return "<span class='el-tag' title='点击查看所有商品信息'>"+value+"</span>";
                }
            },
            events:{
                getItems:{
                    'click .el-tag': function (e, value, row, index) {
                        e.stopPropagation();
                        Fast.api.ajax({
                                url: 'warehouse/ininventory/items',
                                data: {id: row.inorderid}
                            }, function (data, ret) {
                                var items = "";
                                for(i=0;i<data.length;i++){
                                    items += "<span>"+data[i]['itemname']+" - " + data[i]['itemsku']+ " * "+ data[i]['goods_count']+"</span><br/>";
                                }
                                Layer.alert(items);
                                return false;
                            },function (data,ret) {
                                alert(ret.msg);
                            }
                        );

                    }
                }
            }
        }
    };
    return Controller;
});